package com.ysxsoft.wdg.ui.activity.my.partner;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.umeng.socialize.UMShareAPI;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.home.TbkAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.response.HomeProductResponse;
import com.ysxsoft.wdg.bean.response.TuiGuangResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.BigDecimalUtil;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ViewUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.util.statusbar.StatusBarUtil;
import com.ysxsoft.wdg.util.transform.FastSpeedLinearLayoutManager;
import com.ysxsoft.wdg.widget.SharePopupWindow;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class TgActivity extends BaseActivity {

    @BindView(R.id.title_finish)
    ImageView titleFinish;
    @BindView(R.id.title_iv_r)
    ImageView titleIvR;
    @BindView(R.id.title_tv_r)
    TextView titleTvR;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.ll_refresh)
    LinearLayout llRefresh;
    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    TbkAdapter adapter;
    private int page = 1;

    public static void start(Context context) {
        Intent intent = new Intent(context, TgActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tg;
    }

    @Override
    protected void initData() {
        StatusBarUtil.setStatusBarTextColor(this, true);
        initStatusBar(topView, true);
        titleContent.setText("推广");

        recyclerview.setNestedScrollingEnabled(false);
        recyclerview.setFocusable(false);
        recyclerview.setLayoutManager(new FastSpeedLinearLayoutManager(TgActivity.this));
        adapter = new TbkAdapter(R.layout.item_tg);
        recyclerview.setAdapter(adapter);
        mShareAPI = UMShareAPI.get(this);
        popupWindow = new SharePopupWindow(this, mShareAPI);

        getList();
    }

    @Override
    protected void setListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page=1;
                getList();
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                getList();
            }
        });
    }

    @OnClick({R.id.title_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
        }
    }

    /**
     * 获取精选
     */
    private void getList() {
        if (page == 1) {
            multStatusView.showLoading();
        }
        OkHttpUtils.post()
                .url(UrlHost.TUI_GUANG)
                .addParams("uid",  SharePrefUtils.getUserId())
                .addParams("page", page + "")
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                    multStatusView.showError();
                    reset();
                }
            }

            @Override
            public void onResponse(String response, int id) {
                reset();
                if (response != null) {
                    TuiGuangResponse tuiGuangResponse = JsonUtils.parseByGson(response, TuiGuangResponse.class);
                    if (tuiGuangResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(tuiGuangResponse.getCode())) {
                        List<TuiGuangResponse.DataBean> list = tuiGuangResponse.getData();
                        if (page == 1) {
                            multStatusView.showContent();
                            if (list != null && !list.isEmpty()) {
                            } else {
                                multStatusView.showEmpty();
                            }
                            adapter.setNewData(list);
                        } else {
                            adapter.addData(list);
                        }
                    }
                } else {
                    multStatusView.showError();
                }
            }
        });
    }

    private void reset() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh(true);
        }
        refreshLayout.finishLoadmore();
    }


    class TbkAdapter extends BaseQuickAdapter<TuiGuangResponse.DataBean, BaseViewHolder> {
        public TbkAdapter(int layoutResId) {
            super(layoutResId);
        }

        public void clear() {
            getData().clear();
            notifyDataSetChanged();
        }

        @Override
        protected void convert(BaseViewHolder helper, TuiGuangResponse.DataBean item) {
            ImageView imageView=helper.getView(R.id.icon);
            Glide.with(TgActivity.this).load(item.getPic()).into(imageView);

            TextView tv_share=helper.getView(R.id.tv_share);
            tv_share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //TODO：need分享
                    popupWindow.setShareData(getString(R.string.app_name), item.getTitle()/*+data*/, item.getUrl(), "", 0);
                    popupWindow.show(mRootView);
                }
            });
        }
    }

    private UMShareAPI mShareAPI;
    private SharePopupWindow popupWindow;
}
